
             
/**
 * 
 *
 * @File name:  QueryTFixedAssetsByPagerAction.java 
 * @Create on:  2011-10-30 20:01:343
 * @Author   :  陈伟
 *
 * @ChangeList
 * ---------------------------------------------------
 * Date         Editor              ChangeReasons
 *
 */
           
        
package com.boho.estates.report.action;

            
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.xvolks.jnative.JNative;
import org.xvolks.jnative.Type;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import com.boho.base.dao.DaoHelper;
import com.boho.common.Constant;
import com.boho.common.ContentTypeUtils;
import com.boho.common.IoUtil;
import com.boho.common.ReadWriteFile;
import com.boho.common.SystemUtils;
import com.boho.estates.po.*;

import cn.boho.framework.actions.ActionImp;
import cn.boho.framework.context.ActionContext;
import cn.boho.framework.po.POUtils;
import cn.boho.framework.service.FileServerService;
import cn.boho.framework.service.MessageService;
import cn.boho.framework.exception.UserOperationException;
import cn.boho.framework.fileserver.UploadFilePO;
import cn.boho.framework.utils.DateUtils;
import cn.boho.framework.po.DynaBeanMap;
import cn.boho.framework.po.ComboPager;
import cn.boho.framework.po.Pager;
            
        
import com.boho.estates.report.dao.TFixedAssetsDao;
import com.boho.estates.report.service.TFixedAssetsService;
import com.boho.estates.usual.dao.TCheckInfoDao;
import com.boho.estates.usual.service.UsualService;

public class FileUpDownloadAction extends ActionImp {
	
	String check_no = "";
	String oper_type = "";
	private UploadFilePO f1 = null;
	@Override
	protected void doException(ActionContext atx, Exception ex) {
		
		if (!(ex instanceof UserOperationException)) {
			atx.setErrorContext("BOHO_COMMON_1001", "资产盘点"+ex.getMessage(),"【系统后台用户】", ex);
		}
		if (f1 != null && !f1.getFileName().equals("")) {
			f1.deleteLocal();
		}
	}
	@Override
	protected int performExecute(ActionContext atx) throws Exception {
		
		return 1;

	}
	@Override
	protected int verifyParameters(ActionContext atx) throws Exception {
		check_no = atx.getStringValue("CHECK_NO");
		oper_type = atx.getStringValue("OPER_TYPE");
		String filepath = "";
		String createBy = SystemUtils.getSessionUser(atx);
		
		if("D".equals(oper_type)){
			String filename="pandian-"+System.currentTimeMillis()+".txt";
			HttpServletRequest request = (HttpServletRequest)atx.getHttpRequest();
			filepath = request.getRealPath("");
			filepath = filepath.replace("\\", "/");
			filepath = filepath+"/temp";
			File f = new File(filepath);
			if (!f.exists()) {f.mkdirs();}
			filepath = filepath+File.separator+filename;
			
			Pager pager=null;
			pager = new Pager(10000000, 0);
			Connection con=null;
			con = atx.getConection();
			String departIds  = (String)atx.getSessionValue(Constant.SESSION_DEPART_ARRAY);
			ComboPager cp=TCheckInfoDao.queryTCheckInfoDetailByPager(con,pager,check_no,"","0",departIds);
			DaoHelper.setTDepartmentField(con, cp, "", "DEPARTMENT_NAME");
			DaoHelper.setTpersonField(con, cp, "", "WITH_MANANGER");
			
			List list = cp.getRs();
			StringBuffer sb = new StringBuffer();
			for(int i=0;i<list.size();i++){
				
				Map po = (Map)list.get(i);
				sb.append(po.get("BARCODE")).append(",");//条码
				sb.append(po.get("FIXED_ASSETS_NAME")).append(",");//资产名称
				sb.append(po.get("DEPARTMENT_NAME")).append(",");//部门名称
				sb.append(po.get("STORED_IN")).append(",");//存放地点
				sb.append(po.get("WITH_MANANGER")).append(",");//管理人
				sb.append(po.get("IS_CHECK")).append("\r\n");//是否已盘点
				
			}
			ReadWriteFile.writeGBKFile(filepath, sb.toString());
			
		}else{
			
			f1 = atx.getUploadFilePO("FILE");
			if(f1!=null && !f1.getFileName().equals("")){
				
			if("txt".equalsIgnoreCase(f1.getFileExtendName())){
				f1.write(atx, false);//write(atx)write(atx,false);//由配置文件决定是用FTP，还是存在本地
				String absPath = f1.getAbsoluteUrl();
				
				File f = new File(absPath);
				InputStreamReader read = new InputStreamReader(new FileInputStream(f), "UTF-8");
				BufferedReader br = new BufferedReader(read);
				String brStr = "";
				Connection con = atx.getConection();
				while((brStr = br.readLine()) != null){
					String tempStr = brStr;
					String[] arr = tempStr.split(",");//8801003955166,桌子,一部,房间1,张三,0
					if("1".equals(String.valueOf(arr[5]))){
						String  idAndBarcode = String.valueOf(arr[0]);
						TCheckInfoDetailPO tCheckInfoDetailPO = new TCheckInfoDetailPO();
						TCheckInfoDetailPO vCheckInfoDetailPO = new TCheckInfoDetailPO();
						
						tCheckInfoDetailPO.setCheckNo(check_no);
						tCheckInfoDetailPO.setBarcode(idAndBarcode);
						
						vCheckInfoDetailPO.setIsCheck(1);
						vCheckInfoDetailPO.setCheckPerson("机器盘点");
						vCheckInfoDetailPO.setCheckDate(new java.util.Date());
						vCheckInfoDetailPO.setUpdateBy(createBy);
						vCheckInfoDetailPO.setUpdateDate(new java.util.Date());
						POUtils.update(con, tCheckInfoDetailPO, vCheckInfoDetailPO, false);
						
						TCheckInfoLogPO tchekLog =new TCheckInfoLogPO();
						tchekLog.setBarcode(arr[0]);
						tchekLog.setCheckNo(check_no);
						tchekLog.setCheckPerson("机器盘点");
						tchekLog.setCreateBy(createBy);
						tchekLog.setCreateDate(new java.util.Date());
						POUtils.insert(con, tchekLog,false);
					}
				}
				br.close();
				read.close();
				if(f.exists()){
					f.delete();
				}
			}else{
				UserOperationException uoe = new UserOperationException();
				 atx.setErrorContext("FSTI_COMMON_1004", "上传的文件的格式必须是 txt型！",uoe);
				 throw uoe;
			}
			}else{
				 UserOperationException uoe = new UserOperationException();
				 atx.setErrorContext("FSTI_COMMON_1004", "上传的文件不可以为空！",uoe);
				 throw uoe;
			}
		}
		atx.setValue("T_FILE", filepath);
		return 1;
	}
	
	
}
